package com.augurit.gzcgwsp.common.util;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import sun.misc.BASE64Encoder;

import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.sendNotice.foresee.gzcgw.webservice.util.WSInvoker;
import com.augurit.wf.common.entity.SMToSend;
import com.foresee.spiderweb.console.security.bean.ServiceResult;



/**
 * 短信发送客户端
 * @author Administrator
 *
 */
public class SmsWebService {
	private static Logger log=Logger.getLogger(SmsWebService.class);
	
	public static String sendSms(SMToSend entity){
				WSInvoker wsInvoker = new WSInvoker();
				Locale locale = Locale.getDefault(); 
				ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
				String url = localResource.getString("smsurl");
				BASE64Encoder base64 = new BASE64Encoder();
				StringBuffer sb = new StringBuffer();
				InetAddress addr = null;
				try {
					addr = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				String ip=addr.getHostAddress().toString();//获得本机IP
				sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
				sb.append("<info>");
				sb.append("<senderCode>000003</senderCode>");
				sb.append("<userIp>+"+ip+"</userIp>");
				sb.append("<senderIp>CGWXZSP</senderIp>");
				sb.append("<content>"+entity.getSmContent()+"</content>");
				sb.append("<mobiles>"+entity.getDestAddr()+"</mobiles>");
				sb.append("<sendTime>"+DateUtilsEX.dateFormate(new Date(), "yyyy-MM-dd HH:mm:ss")+"</sendTime>");
				sb.append("<sendPhone>"+entity.getDestAddr()+"</sendPhone>");
				sb.append("<loginName>aosadmin</loginName>");
				sb.append("</info>");
				String para = "";
				try {
					para = new String(base64.encode(sb.toString().trim().getBytes("UTF-8")));
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				ServiceResult serviceResult = null;
				try {
					if(!ip.contains("192.168.")){
						serviceResult = wsInvoker.invokeWS(url, "GZCGW_SMS_Service", "sendSms", new Object[]{para});
						log.info("返回结果---------"+serviceResult.getRetMessage());
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				if(serviceResult != null) {
					String result = serviceResult.getRetMessage();
		           return result;
				} else {
					System.out.println("WS invoked failed");
				}
			return null;
		
	}

	
	public static void main(String [] args){
		SMToSend entity=new SMToSend();
		entity.setDestAddr("15986300525");
		entity.setSmContent("test");
		SmsWebService.sendSms(entity);
	}
	
}
